﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using Valentia.Kinection.DataTypes;
using Valentia.Kinection.Configuration;

namespace Valentia.Kinection.Service.Manager
{
    static class Program
    {
        public static readonly String ApplicationName = "Kinection Service Manager";
        public static readonly String SysTrayName = "Valentia Kinection";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool lockAquired = false;
            using (System.Threading.Mutex objThread = new System.Threading.Mutex(true, "e9ba6835-9af7-4942-a82c-4b7c44d5e6f0", out lockAquired))
            {
                if (!lockAquired)
                {
                    MessageBox.Show(
                        "Valentia Kinection Service Manager is already running", Program.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                bool logEnabled = true; String logDir = String.Empty;
                try
                {
                    List<ServiceSetting> serviceSettings = ConfigurationHelper.GetServiceSettings();
                    if (serviceSettings == null || serviceSettings.Count <= 0)
                    {
                        Logger.LogFolder = (serviceSettings[0].LogDirectory ?? String.Empty).Trim();
                        Logger.LoggingEnabled = serviceSettings[0].IsLoggingEnabled;
                    }
                }
                catch { }

                Logger.LogFolder = logDir;
                Logger.LoggingEnabled = logEnabled;
                Logger.ApplicationName = "Valentia Kinection Service Manager"; //KinectService.ThisServiceName;

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new ServiceManager());
            }
        }
    }
}